package com.house365.core.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;

import com.house365.core.R;
import com.house365.core.action.ActionTag;
import com.house365.core.action.TabChangeReceiver;
import com.house365.core.action.TabChanger;
import com.house365.core.anim.AnimBean;
import com.house365.core.anim.AnimCommon;
import com.house365.core.application.BaseApplication;
import com.house365.core.constant.CorePreferences;
import com.house365.core.image.AsyncImageLoader;
import com.house365.core.image.CacheImageUtil;
import com.house365.core.image.CacheImageUtil.TextImagePosition;
import com.house365.core.image.ImageLoadedCallback;
import com.house365.core.touch.ImageViewTouch;
import com.house365.core.view.LoadingDialog;

/**
 * @author py
 * @date 2012-5-23
 * @comment
 */
public abstract class BaseTabActivity extends TabActivity implements TabChanger {
	protected BaseApplication mApplication;
	private TabChangeReceiver tabChangeReceiver;
	private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			CorePreferences.DEBUG("onReceive: " + intent);
			finish();
		}
	};

	protected Activity thisInstance; 
	protected LoadingDialog tloadingDialog ;
	protected AlertDialog.Builder alertDialog;
	
	private AsyncImageLoader mAil;
	
	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mApplication = (BaseApplication) getApplication();
		registerReceiver(mLoggedOutReceiver, new IntentFilter(
				ActionTag.INTENT_ACTION_LOGGED_OUT));
		if (tabChangeReceiver == null) {
			tabChangeReceiver = new TabChangeReceiver(this);
			registerReceiver(tabChangeReceiver, new IntentFilter(
					ActionTag.INTENT_ACTION_CHANGE_TAB));
		}

		preparedCreate(savedInstanceState);

	}

	protected abstract void preparedCreate(android.os.Bundle savedInstanceState);

	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mLoggedOutReceiver);
//		mApplication.onAppCancel();
		if (tabChangeReceiver != null) {
			unregisterReceiver(tabChangeReceiver);
		}
	}

	public void addTab(String tag, View view, Intent intent) {
		TabSpec tabspec = getTabHost().newTabSpec(tag);
		tabspec.setContent(intent);
		tabspec.setIndicator(view);
		getTabHost().addTab(tabspec);
	}

	@Override
	public void changeTab(int tab) {
		getTabHost().setCurrentTab(tab);
	}

	@Override
	protected void onPause() {
		if (AnimCommon.in != 0 && AnimCommon.out != 0) {
			super.overridePendingTransition(AnimCommon.in, AnimCommon.out);
			AnimCommon.clear();
		}
		super.onPause();
	}
	
	
	
	
	
	private LoadingDialog getLoadingDialog(){
		if(tloadingDialog == null){
			tloadingDialog= new LoadingDialog(this,
					R.style.dialog, R.string.loading);
			tloadingDialog.setCancelable(isCancelDialog());
			if(isCancelDialog()){
				tloadingDialog.setOnCancelListener(new OnCancelListener() {
					
					@Override
					public void onCancel(DialogInterface dialog) {
						onExitDialog();
					}
				});
			}
		
		}
		return tloadingDialog;
	}
	public void showLoadingDialog(){
		if(getLoadingDialog() != null){
			getLoadingDialog().show();
		}
	}
	public void showLoadingDialog(int resid){
		if(getLoadingDialog() != null){
			getLoadingDialog().setMessage(this.getResources().getString(resid));
			getLoadingDialog().show();
		}
	}
	public void dismissLoadingDialog(){
		if(getLoadingDialog() != null){
			getLoadingDialog().dismiss();
		}
	}
	public AlertDialog.Builder getAlertDialog(){
		if(alertDialog == null){
			alertDialog = new AlertDialog.Builder(this);
		}
		return alertDialog;
	}
	
	public void showToast(String str){
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	}
	public void showToast(int resId){
		Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
	}
	public void onExitDialog(){
		thisInstance.finish();
	}
	public boolean isCancelDialog(){
		return true;
	}
	
	// lzq start
		public void setImage(final ImageView imageView, String imageUrl,
				final int resId, int scaleType) {
			CacheImageUtil.setCacheImage(imageView, imageUrl, resId, scaleType,
					getImageLoader());
		}
		
		// liuziqiang overload setImage
		public void setImage(final ImageView imageView, String imageUrl, int scaleType, final ImageLoadedCallback imgLoadObj) {
			CacheImageUtil.setCacheImage(imageView, imageUrl, scaleType, getImageLoader(), imgLoadObj);
		}

		public void setTextImage(final TextView textView,
				TextImagePosition position, String imageUrl) {
			CacheImageUtil.setCacheTextImage(textView, position, imageUrl,
					getImageLoader());
		}

		public void setTouchImage(final ImageViewTouch imageView, String imageUrl,
				final int resId, int scaleType) {
			CacheImageUtil.setTouchImage(imageView, imageUrl, getResources(),
					resId, scaleType, getImageLoader());
		}

		public AsyncImageLoader getImageLoader() {
			if (mAil == null) {
				mAil = new AsyncImageLoader(this);
			}
			return mAil;
		}
		//lzq end
		
	public void finish() {
		super.finish();
		AnimBean animBean = getFinishAnim();
		if (animBean != null && animBean.getIn() > 0 && animBean.getOut() > 0) {
			overridePendingTransition(animBean.getIn(), animBean.getOut());
		}
	}

	@Override
	public void startActivity(Intent intent) {
		super.startActivity(intent);
		AnimBean animBean = getStartAnim();
		if (animBean != null && animBean.getIn() > 0 && animBean.getOut() > 0) {
			overridePendingTransition(animBean.getIn(), animBean.getOut());
		}
	}

	@Override
	public void startActivityForResult(Intent intent, int requestCode) {
		// TODO Auto-generated method stub
		super.startActivityForResult(intent, requestCode);
		AnimBean animBean = getStartAnim();
		if (animBean != null && animBean.getIn() > 0 && animBean.getOut() > 0) {
			overridePendingTransition(animBean.getIn(), animBean.getOut());
		}
	}

	public AnimBean getStartAnim() {
//		return new AnimBean(R.anim.slide_in_bottom, R.anim.slide_fix);
	return new AnimBean(R.anim.slide_in_right, R.anim.slide_fix);
}

public AnimBean getFinishAnim() {
		//return new AnimBean(R.anim.slide_fix, R.anim.slide_out_bottom);
	return new AnimBean(R.anim.slide_fix, R.anim.slide_out_right);
}
	
}
